package com.ruoyi.web.sso.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.sso.domain.Card;
import com.ruoyi.web.sso.service.ICardService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统卡Controller
 * 
 * @author qiufeng
 * @date 2022-10-27
 */
@Controller
@RequestMapping("/sso/card")
public class CardController extends BaseController
{
    private String prefix = "sso/card";

    @Autowired
    private ICardService cardService;

    @RequiresPermissions("sso:card:view")
    @GetMapping()
    public String card()
    {
        return prefix + "/card";
    }

    /**
     * 查询系统卡列表
     */
    @RequiresPermissions("sso:card:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Card card)
    {
        startPage();
        List<Card> list = cardService.selectCardList(card);
        return getDataTable(list);
    }

    /**
     * 导出系统卡列表
     */
    @RequiresPermissions("sso:card:export")
    @Log(title = "系统卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Card card)
    {
        List<Card> list = cardService.selectCardList(card);
        ExcelUtil<Card> util = new ExcelUtil<Card>(Card.class);
        return util.exportExcel(list, "系统卡数据");
    }

    /**
     * 新增系统卡
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统卡
     */
    @RequiresPermissions("sso:card:add")
    @Log(title = "系统卡", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Card card)
    {
        return toAjax(cardService.insertCard(card));
    }

    /**
     * 修改系统卡
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Card card = cardService.selectCardById(id);
        mmap.put("card", card);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统卡
     */
    @RequiresPermissions("sso:card:edit")
    @Log(title = "系统卡", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Card card)
    {
        return toAjax(cardService.updateCard(card));
    }

    /**
     * 删除系统卡
     */
    @RequiresPermissions("sso:card:remove")
    @Log(title = "系统卡", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cardService.deleteCardByIds(ids));
    }


    @Log(title = "导入系统卡", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Card> util = new ExcelUtil<Card>(Card.class);
        List<Card> cardList = util.importExcel(file.getInputStream());
        String message = cardService.importCard(cardList, getLoginName());
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Card> util = new ExcelUtil<Card>(Card.class);
        return util.importTemplateExcel("系统卡号数据");
    }
}
